package com.tmeone.erpone.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.glassfish.gmbal.ParameterNames;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.SCompany;
import com.tmeone.erpone.entity.SCompanys;
import com.tmeone.erpone.entity.TStaff;
import com.tmeone.erpone.service.ISCompanyService;
import com.tmeone.erpone.service.ISCompanysService;
import com.tmeone.erpone.service.ITStaffService;
import org.apache.catalina.Session;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class qiyeToCompany {

    @Autowired
    private ISCompanysService companysService;

    @Autowired
    private ISCompanyService companyService;


    @Autowired
    private ITStaffService staffService;
    /**
     * 企业to员工中企业Id
     */
    private String qiYeToStaffId;
    /**
     * 企业to员工to详情页面
     */
    private  String qiYeToDetailsId;



    @GetMapping("/qiToCompanys")
    public IPage<SCompanys> findPage(@RequestParam(
            value = "current", required = false, defaultValue = "1") Integer current) {

        Page<SCompanys> page = new Page<>(current, 5);
        //传入分页参数，获得分页结果
        IPage<SCompanys> result = companysService.getQiToCompany(page);
        for (SCompanys record : result.getRecords()) {
            record.setStaffNumber(companysService.selectStaffNumber(record.getId()));
            record.setStaffNumberYes(companysService.selectStaffNumberYes(record.getId()));
            record.setRoleId("1");
        }

        return result;
    }

    @GetMapping("/qiToCompanysToMo/{current}/{role}/{faRen}/{name}/{status2}")
    public JsonResult findPageMoHu(@PathVariable("current") Integer current, @PathVariable("role") Integer role, @PathVariable("faRen") String faRen
            , @PathVariable("name") String name, @PathVariable("status2") String status) {
        if(name.equals("null")){
            name="";
        }
        if(faRen.equals("null")){
            faRen="";
        }
        Page<SCompanys> page = new Page<>(current, 5);
        IPage<SCompanys> result = companysService.getQiToCompany2(page, role, name, faRen, status);
        if (role==0){
            for (SCompanys record : result.getRecords()) {
                record.setStaffNumber(companysService.selectStaffNumber(record.getId()));
                record.setStaffNumberYes(companysService.selectStaffNumberYes(record.getId()));
                System.out.println(record.getRoleId());
                record.setRoleId("0");
            }
            return new JsonResult(1, result);
        }else {
            for (SCompanys record : result.getRecords()) {
                record.setStaffNumber(companysService.selectStaffNumber(record.getId()));
                record.setStaffNumberYes(companysService.selectStaffNumberYes(record.getId()));
                record.setRoleId("1");
            }
            return new JsonResult(1, result);
        }



    }


    /**
     * 改变公司的状态
     *
     * @return
     */
    @PutMapping("qiToCompanysStatus/{id}")
    public JsonResult changeStatus(@PathVariable("id") String id) {

        SCompany one = companyService.getOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getId, id));
        if (one.getCompanyStatus().equals("on")) {
            one.setCompanyStatus("off");
        } else {
            one.setCompanyStatus("on");
        }
        companyService.updateById(one);
        return new JsonResult(1, null);
    }

    /**
     * 企业ToStaff去存入id值
     */
    @GetMapping("/qiYeToStaff/{id}")
    public JsonResult qiYeToStaff(@PathVariable String id) {
        this.qiYeToStaffId = id;
        return new JsonResult(1, null);
    }



    @ResponseBody
    @RequestMapping("/qiYeToStaff/{current}/{input}/{dId}/{pageSize}")
    private IPage<TStaff> selectListStaff(@PathVariable("current") Integer current, @PathVariable("input") String name, @PathVariable("dId") String dId, @PathVariable("pageSize") Integer pageSize) {

        Page<TStaff> tStaffPage = new Page<>(current, pageSize);

        IPage<TStaff> page = staffService.getPageQiYeToTStaff(tStaffPage, qiYeToStaffId);

        return  page;

    }
    /**
     * qiYeToDetails  企业to员工to详情页面 中员工id
     *
     * @param
     * @return
     */
    @GetMapping("/qiYeToDetails/{id}")
    private void qiYeToDetails(@PathVariable String id){
        this.qiYeToDetailsId = id;
    }

    /**
     * 员工基本信息
     */

    @GetMapping("/qiYeToDetailsAll")
    private JsonResult  qiYeToDetailsAll(){

       List<TStaff> tstaff= staffService.getStaffById(this.qiYeToDetailsId);

        return  new JsonResult(1,tstaff);
    }

}
